package com.campus.controller;

import com.campus.model.TeachingStudentLearning;
import com.campus.service.TeachingStudentLearningService;
import com.campus.web.vo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/homework/studentLearning")
public class TeachingStudentLearningController {
    @Autowired
    private TeachingStudentLearningService teachingStudentLearningService;

    /**
     * 新增学生学习记录
     */
    @PostMapping("/add")
    public ResponseResult<String> createTeachingStudentLearning(@RequestBody TeachingStudentLearning studentLearning) {
        boolean success = teachingStudentLearningService.createTeachingStudentLearning(
                studentLearning.getStuInClassId(), studentLearning.getUnitId(),
                studentLearning.getFinishedStatus(), studentLearning.getFinishedTime()
        );
        return success
                ? ResponseResult.ok("Student learning record created successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to create student learning record");
    }

    /**
     * 修改学生学习记录
     */
    @PutMapping("/update/{id}")
    public ResponseResult<String> updateTeachingStudentLearning(
            @PathVariable int id, @RequestBody TeachingStudentLearning studentLearning) {
        boolean success = teachingStudentLearningService.updateTeachingStudentLearning(
                id, studentLearning.getFinishedStatus(), studentLearning.getFinishedTime()
        );
        return success
                ? ResponseResult.ok("Student learning record updated successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to update student learning record");
    }

    /**
     * 删除学生学习记录
     */
    @DeleteMapping("/delete/{id}")
    public ResponseResult<String> deleteTeachingStudentLearning(@PathVariable int id) {
        boolean success = teachingStudentLearningService.deleteTeachingStudentLearning(id);
        return success
                ? ResponseResult.ok("Student learning record deleted successfully")
                : ResponseResult.error(HttpStatus.INTERNAL_SERVER_ERROR, "Failed to delete student learning record");
    }

    /**
     * 根据学习单元ID查询所有学生学习记录
     */
    @GetMapping("/listByUnit/{unitId}")
    public ResponseResult<List<TeachingStudentLearning>> listAllStudentLearningByUnitId(@PathVariable int unitId) {
        List<TeachingStudentLearning> studentLearnings = teachingStudentLearningService.listAllStudentLearningByUnitId(unitId);
        return studentLearnings != null && !studentLearnings.isEmpty()
                ? ResponseResult.ok(studentLearnings)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No student learning records found for this unit");
    }

    /**
     * 查询所有学生学习记录
     */
    @GetMapping("/list")
    public ResponseResult<List<TeachingStudentLearning>> listAllStudentLearning() {
        List<TeachingStudentLearning> studentLearnings = teachingStudentLearningService.listAllStudentLearning();
        return studentLearnings != null && !studentLearnings.isEmpty()
                ? ResponseResult.ok(studentLearnings)
                : ResponseResult.error(HttpStatus.NOT_FOUND, "No student learning records found");
    }
}
